package org.example;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{
    public void chat(String message){
        Pattern pattern = Pattern.compile("\"username\":\"(.*?)\"");
        Matcher matcher = pattern.matcher(message);
        Pattern patternme = Pattern.compile("\"message\":\"(.*?)\"");
        Matcher matcherme = patternme.matcher(message);
        String username = "";
        if (matcher.find()) {
            username = matcher.group(1);
        }
        String messageText = "";
        if (matcherme.find()) {
            messageText = matcherme.group(1);
        }

        // SQL 插入语句
        String sql = "INSERT INTO ChatMessages (UserName, MessageText) VALUES (?, ?)";

        // 使用 DBUtil 的 executePreparedUpdate 方法插入数据
        int result = DBUtil.executePreparedUpdate(sql, username, messageText);

        // 根据操作结果进行相应处理
        if (result > 0) {
            System.out.println("消息已成功保存到数据库");
        } else {
            System.out.println("保存消息失败");
        }
    }
}